package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.EquipmentParameter;
import com.chain.qs.modules.biz.service.EquipmentParameterService;
import com.chain.qs.modules.biz.service.EquipmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备参数Controller
 *
 * @author Wang zhiping
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/equParameter")
public class EquipmentParamentsController extends BaseController {

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentParameterService parameterService;

    @ModelAttribute("parameter")
    public EquipmentParameter get(@RequestParam(required = false) String id, @RequestParam(required = false, value = "equipment.id") String equipmentId) {
        EquipmentParameter parament;
        if (StringUtils.isNotBlank(id)) {
            parament = parameterService.get(id);
        } else {
            parament = new EquipmentParameter();
        }
        if (StringUtils.isNotBlank(equipmentId)) {
            parament.setEquipment(equipmentService.get(equipmentId));
        }
        return parament;
    }

    @RequestMapping(value = {"list", ""})
    public String list(@ModelAttribute("parameter") EquipmentParameter parament, Model model) {
        List<EquipmentParameter> list = parameterService.findList(parament);
        System.out.println(list.size());
        model.addAttribute("list", parameterService.findList(parament));
        return "modules/biz/equparameterList";
    }

    @RequestMapping(value = "form")
    public String form() {
        return "modules/biz/equparameterForm";
    }


    @ResponseBody
    @RequestMapping(value = "save")//@Valid
    public String save(EquipmentParameter parameter) {
        if (parameter.getId() == null || parameter.getId().equals("")) {//新增
            if (parameterService.getOne(parameter) != null) {
                return "kidIsExist";
            }
        }
        parameterService.save(parameter);
        return "success";
    }

    @RequestMapping(value = "delete")
    public String delete(@ModelAttribute("parameter") EquipmentParameter parameter, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/equParameter/";
        }
        parameterService.delete(parameter);
        addMessage(redirectAttributes, "删除参数成功");
        return "redirect:" + adminPath + "/biz/equParameter/?equipment.id=" + parameter.getEquipment().getId();
    }

    @RequestMapping(value = "itemParamList")
    public String itemParamList(String itemId, HttpServletResponse response) {
        List<EquipmentParameter> itemParamList = parameterService.itemParamList(itemId);
        return renderString(response, itemParamList);
    }
}
